package com.cloud.test03;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 字符流响应乱码问题    PrintWriter
 * getWrite方法默认的字符集编码无法处理中文
 * 要在服务器和客户端两端都设置编码方式才能解决乱码问题
 *
 * //设置服务器端的编码
 * response.setCharacterEncoding("UTF-8");
 * //设置客户端的解码方式和响应的mime类型
 * response.setHeader(响应类型："content-type","text/html;charset=UTF-8");
 *
 * 客户端和服务端的编码方式都要设置，并且保持一致
 *
 * //同时设置客户端和服务端
 * response.setContentType("text/html;charset=UTF-8");
 * @author DELL
 */
@WebServlet(value = {"/ResTest02", "/restest02"})
public class ResponseTest02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       /* //设置服务器端的编码
        response.setCharacterEncoding("UTF-8");
        //设置客户端的解码方式
        response.setHeader("content-type","text/html;charset=UTF-8");*/

        //同时设置客户端和服务端
        response.setContentType("text/html;charset=UTF-8");

        response.getWriter().write("<h1>你好!</h1>");
        response.getWriter().close();
        //response.getOutputStream().println("Hello again!");
    }
}
